package handler

import (
	"github.com/gin-gonic/gin"
	"honghu_be/common"
	"honghu_be/dal"
	"honghu_be/model"
	"honghu_be/model/vo"
)

func GetBoxes(c *gin.Context) {
	req := &model.GetBoxesRequest{}
	if err := c.ShouldBind(req); err != nil {
		c.JSON(400, err.Error())
		return
	}
	resp, err := getBoxes(c, req)
	if err != nil {
		common.WriteError(c, err)
		return
	}
	c.JSON(200, resp)
}

func getBoxes(c *gin.Context, req *model.GetBoxesRequest) (*model.GetBoxesResponse, error) {
	if req.Limit == 0 {
		req.Limit = 20
	}
	pos, err := dal.GetBoxes(c, &dal.GetBoxesRequest{
		Limit:  req.Limit,
		Offset: req.Offset,
	})
	if err != nil {
		return nil, err
	}

	return &model.GetBoxesResponse{
		Meta:  common.MetaOk,
		Boxes: vo.ToBoxVos(pos),
	}, nil
}
